<template>
  <div class="layout-col plr15 pb15">
    <div class="layout-col bs">
      <div class="ww100 flex-bc plr12 ptb8 bs2">
        <div class="flex-sc">
          <div class="h30"><img class="hh100" src="@/assets/images/tibi-ico.png" /></div>
          <span class="f15 fw ml8">动环日志</span>
        </div>
        <div class="flex-ec cursor flex1">
          <div class="ml15 mr10">设备选择</div>
          <div class="flex w50x4">
            <el-select v-model="state.sensor_id" placeholder="请选择" style="width: 100%" clearable filterable>
              <el-option label="全部" value="" />
              <el-option v-for="(v, i) in state.sensorList" :key="v.id" :label="v.name" :value="v.id" />
            </el-select>
          </div>
          <div class="ml15 mr10">时间范围</div>
          <div class="flex w50x8">
            <el-date-picker v-model="state.datetime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
              type="datetimerange" range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间" />
          </div>
          <el-button class="bgi3 white ml15" @click.stop="state.page = 1; init()" :icon="Search">搜索</el-button>
        </div>
      </div>
      <div class="row bgi22">
        <div :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i" v-show="v.show" :class="typeof(v.key) == 'object'?v.width +' '+ 'mlr5':v.width">{{v.name}}</div>
      </div>
      <div class="table">
        <div class="row" :class="publicStore.active[state.key] == item[state.key] || item['click']?'active':index%2 == 1?'bgi23':'bgi18'" v-for="(item, index) in state.list" :key="index" @click="handleClick('active', item)">
          <div v-show="v.show" :class="v.width" :style="{textAlign: v.align}" v-for="(v, i) in state.content" :key="i">
            <span v-if="typeof(v.key) == 'object'" class="i21 cursor mlr5" :class="kk=='add'?'none':kk=='deal'&&item['clear_type']!=0?'none':''" v-for="(vv, kk, ii) in v.key" :key="ii" @click.stop="handleClick(kk, item)">{{vv}}</span>
            <span v-else-if="v.key=='*'">{{index+1}}</span>
            <span v-else-if="v.type == 'time'">{{item[v.key] == 0?'/':parseTime(item[v.key])}}</span>
            <span v-else-if="v.type == 'find'" :class="find(v.value[0], [v.value[1], item[v.key]], 'color')?find(v.value[0], [v.value[1], item[v.key]], 'color'):''">
              {{find(v.value[0], [v.value[1], item[v.key]], v.value[2])}}
            </span>
            <span v-else-if="v.type == 'switch'">
              <el-switch v-model="item[v.key]" :active-text="v.value[1].name" :inactive-text="v.value[0].name" :active-value="v.value[1].value" :inactive-value="v.value[0].value" inline-prompt @change="handleClick('switch', item)"></el-switch>
            </span>
            <span v-else-if="v.type == 'input'"><el-input v-model="item[v.value]" placeholder="请输入" style="width: 150px;" size="small" /></span>
            <span v-else>{{isNull(item[v.key]) && v.key!=''? '/' : item[v.key]}}</span>
          </div>
        </div>
        <div v-if="state.empty" class="flex-cc ww100 tc ptb13 bgi18">没有数据</div>
      </div>
      <Pagination class="plr20" v-show="state.total>0" :total="state.total" v-model:page.sync="state.page" v-model:limit.sync="state.limit" @pagination="init" />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { Refresh, Search, Filter, Connection, Edit, Operation } from '@element-plus/icons-vue'
  import Pagination from '@/components/Pagination'
  const route = useRoute()
	const { proxy }:any = getCurrentInstance()
  const publicStore = proxy.publicStore()
  const configStore = proxy.configStore()
  let resultList = $ref([
    {value: '0', name: '未操作', color: 'ca'},
    {value: '1', name: '成功', color: 'i21'},
    {value: '2', name: '失败', color: 'i8'},
  ])
  const state = reactive({
	  ...publicStore.$state,
    key: 'id',
    query: {"o": "o_sensor_control_log"},
    content: [
      { width: 'w70',   show: true, align: 'center', key: '*', name: '序号' },
      { width: 'w50x5', show: true, align: 'left',  key: 'sensor_name', name: '设备名称' },
      { width: 'w50x6', show: true, align: 'left',  key: 'instlPos', name: '安装位置' },
      { width: 'w50x4', show: true, align: 'left',  key: 'code_name', name: '遥控名称' },
      { width: 'w50x4', show: true, align: 'left',  key: 'action_name', name: '动作名称' },
      { width: 'w50x4', show: true, align: 'left',  key: 'startTime', name: '开始时间', type: 'time' },
      { width: 'w50x4', show: true, align: 'left',  key: 'endTime', name: '结束时间', type: 'time' },
      { width: 'w50x4', show: true, align: 'left',  key: 'userName', name: '操作人员' },
      { width: 'w50x4', show: true, align: 'left',  key: 'result', name: '操作结果', type: 'find', value: [resultList, 'value', 'name'] },
      { width: 'w50x4', show: true, align: 'right', key: {view: '查看'},  name: '操作' },
    ],
		sensorList: [],
    sensor_id: '',
  })
  let exposeRef = $ref()
 
  onMounted(async() => {
    state.form = route.query
    await getInit()
    await init()
  })

  const getInit = async () => {
    let query =  {"o": "o_sensor", "p": [publicStore.distributId], "i": 0}
    let params = {queryApi: query}
    let res = await publicStore.post(params)
    state.sensorList = proxy.isNull(res)? [] : res.filter(a=>a.class == 0)
  }

  const init = async () => {
    getQuery()
    state.params = {queryApi: Object.assign({s: (state.page -1)*state.limit, n: state.limit}, state.query), queryApiCount: state.query}
    let res = await publicStore.post(state.params)
    state.list = proxy.isNull(res.queryApi)? [] : res.queryApi
    state.empty = proxy.isNull(res.queryApi)? true : false
    state.total = proxy.isNull(res.queryApiCount)? 0 : parseInt(res.queryApiCount[0].count)
  }

    // 检索条件
  const getQuery  = () => {
    if(!state.sensor_id && proxy.isNull(state.datetime)){
      state.query.p = [publicStore.distributId]
      state.query.i = 0
    }
    // 检索设备
    if(state.sensor_id && proxy.isNull(state.datetime)){
      state.query.p = [publicStore.distributId, state.sensor_id]
      state.query.i = 1
    }
    // 检索时间段
    if(!state.sensor_id && !proxy.isNull(state.datetime)){
      let start = Date.parse(state.datetime[0]) + ''
      let end = Date.parse(state.datetime[1]) + ''
      state.query.p = [publicStore.distributId, start, end]
      state.query.i = 3
    }
    // 传感器 + 时间段
    if(state.sensor_id && !proxy.isNull(state.datetime)){
      let start = Date.parse(state.datetime[0]) + ''
      let end = Date.parse(state.datetime[1]) + ''
      state.query.p = [publicStore.distributId, state.sensor_id, start, end]
      state.query.i = 4
    }
  }

  const handleClick = (remark, val) => {
    if(remark == 'view'){
      let value = state.sensorList.find(a=>a.id == val.sensor_id)
      let query = {id: val.sensor_id, name: val.sensor_name, type: value.type, instlPos: value.instlPos}
      proxy.toPath('/chart', query)
    }
  }

</script>
  
<style scoped lang="scss">

</style>
  